package cn.edu.nuc.ehelper;

import cn.edu.nuc.dictFile.copy2SD;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EheplerActivity extends Activity {
	// public static String DICT_PATH = "";
//	private final String TAG = "EheplerActivity";
//	private boolean DEBUG = true;
	private EditText queryEdit;
	private Button wordsBook;
	private Button review;
	private Button searchButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		String mnt = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/dictionary";
		copy2SD copy = new copy2SD(this);
		copy.loadDictFile(mnt);

		queryEdit = (EditText) findViewById(R.id.queryEditTextId);
		wordsBook = (Button) findViewById(R.id.wordsBookButtonId);
		review = (Button) findViewById(R.id.reviewButtonId);
		searchButton = (Button) findViewById(R.id.startFind);
		searchButton
				.setBackgroundResource(android.R.drawable.ic_search_category_default);

		OnClickListener mainListen = new ButtonListener();
		searchButton.setOnClickListener(mainListen);
		wordsBook.setOnClickListener(mainListen);
		review.setOnClickListener(mainListen);

		queryEdit.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

				// TODO Auto-generated method stub
				//System.out.println(s);
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	class ButtonListener implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.wordsBookButtonId:
				Intent wordsBookIntent = new Intent(EheplerActivity.this,
						WordsBookActivity.class);
				startActivity(wordsBookIntent);
				break;
			case R.id.reviewButtonId:
				Intent reviewIntent = new Intent(EheplerActivity.this,
						ReviewActivity.class);
				startActivity(reviewIntent);
				break;
			case R.id.startFind:
				String word_tmp = queryEdit.getText().toString();
				if (word_tmp.isEmpty()) {
					Toast.makeText(getApplicationContext(), "输入单词",
							Toast.LENGTH_SHORT).show();
				} else {
					Intent searchIntent = new Intent(EheplerActivity.this,
							QueryResultActivity.class);
					searchIntent.putExtra("words", queryEdit.getText()
							.toString());
					startActivity(searchIntent);
				}
			default:
				break;
			}

		}
	}

}